//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imagemanager.util;

import android.database.AbstractCursor;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.util.Log;

public class SortCursor extends AbstractCursor {
    private static final String TAG = "SortCursor";
    private Cursor mCursor;
    private Cursor[] mCursors;
    private int[] mSortColumns;
    private final int ROWCACHESIZE = 64;
    private int[] mRowNumCache = new int[64];
    private int[] mCursorCache = new int[64];
    private int[][] mCurRowNumCache;
    private int mLastCacheHit = -1;
    private int mType;
    private boolean mAscending;
    public static final int TYPE_STRING = 0;
    public static final int TYPE_NUMERIC = 1;
    private DataSetObserver mObserver = new DataSetObserver() {
        public void onChanged() {
            SortCursor.this.mPos = -1;
        }

        public void onInvalidated() {
            SortCursor.this.mPos = -1;
        }
    };
    private int mCursorIndex;

    public SortCursor(Cursor[] cursors, String sortcolumn, int type, boolean ascending) {
        this.mAscending = ascending;
        this.mCursors = cursors;
        this.mType = type;
        int length = this.mCursors.length;
        this.mSortColumns = new int[length];

        int i;
        for(i = 0; i < length; ++i) {
            if(this.mCursors[i] != null) {
                this.mCursors[i].registerDataSetObserver(this.mObserver);
                this.mCursors[i].moveToFirst();
                this.mSortColumns[i] = this.mCursors[i].getColumnIndexOrThrow(sortcolumn);
            }
        }

        this.mCursor = null;
        if(type == 0) {
            String smallest = "";

            for(int j = 0; j < length; ++j) {
                if(this.mCursors[j] != null && !this.mCursors[j].isAfterLast()) {
                    String current = this.mCursors[j].getString(this.mSortColumns[j]);
                    if(this.mCursor == null || current == null || current.compareToIgnoreCase(smallest) < 0) {
                        smallest = current;
                        this.mCursor = this.mCursors[j];
                        this.mCursorIndex = j;
                    }
                }
            }
        } else {
            long smallest = ascending?9223372036854775807L:-9223372036854775808L;

            for(int j = 0; j < length; ++j) {
                if(this.mCursors[j] != null && !this.mCursors[j].isAfterLast()) {
                    long current = this.mCursors[j].getLong(this.mSortColumns[j]);
                    boolean comparison = ascending?current < smallest:current > smallest;
                    if(this.mCursor == null || comparison) {
                        smallest = current;
                        this.mCursor = this.mCursors[j];
                        this.mCursorIndex = j;
                    }
                }
            }
        }

        for(i = this.mRowNumCache.length - 1; i >= 0; --i) {
            this.mRowNumCache[i] = -2;
        }

        this.mCurRowNumCache = new int[64][length];
    }

    public int getCount() {
        int count = 0;
        int length = this.mCursors.length;

        for(int i = 0; i < length; ++i) {
            if(this.mCursors[i] != null) {
                count += this.mCursors[i].getCount();
            }
        }

        return count;
    }

    public boolean onMove(int oldPosition, int newPosition) {
        if(oldPosition == newPosition) {
            return true;
        } else {
            int cache_entry = newPosition % 64;
            int length;
            if(this.mRowNumCache[cache_entry] == newPosition) {
                length = this.mCursorCache[cache_entry];
                this.mCursor = this.mCursors[length];
                this.mCursorIndex = length;
                if(this.mCursor == null) {
                    Log.w("SortCursor", "onMove: cache results in a null cursor.");
                    return false;
                } else {
                    this.mCursor.moveToPosition(this.mCurRowNumCache[cache_entry][length]);
                    this.mLastCacheHit = cache_entry;
                    return true;
                }
            } else {
                this.mCursor = null;
                length = this.mCursors.length;
                int smallestIdx;
                if(this.mLastCacheHit >= 0) {
                    for(smallestIdx = 0; smallestIdx < length; ++smallestIdx) {
                        if(this.mCursors[smallestIdx] != null) {
                            this.mCursors[smallestIdx].moveToPosition(this.mCurRowNumCache[this.mLastCacheHit][smallestIdx]);
                        }
                    }
                }

                if(newPosition < oldPosition || oldPosition == -1) {
                    for(smallestIdx = 0; smallestIdx < length; ++smallestIdx) {
                        if(this.mCursors[smallestIdx] != null) {
                            this.mCursors[smallestIdx].moveToFirst();
                        }
                    }

                    oldPosition = 0;
                }

                if(oldPosition < 0) {
                    oldPosition = 0;
                }

                smallestIdx = -1;
                int i;
                if(this.mType == 0) {
                    for(i = oldPosition; i <= newPosition; ++i) {
                        String smallest = "";
                        smallestIdx = -1;

                        for(int j = 0; j < length; ++j) {
                            if(this.mCursors[j] != null && !this.mCursors[j].isAfterLast()) {
                                String current = this.mCursors[j].getString(this.mSortColumns[j]);
                                if(smallestIdx < 0 || current == null || current.compareToIgnoreCase(smallest) < 0) {
                                    smallest = current;
                                    smallestIdx = j;
                                }
                            }
                        }

                        if(i == newPosition) {
                            break;
                        }

                        if(this.mCursors[smallestIdx] != null) {
                            this.mCursors[smallestIdx].moveToNext();
                        }
                    }
                } else {
                    for(i = oldPosition; i <= newPosition; ++i) {
                        long smallest = this.mAscending?9223372036854775807L:-9223372036854775808L;
                        smallestIdx = -1;

                        for(int j = 0; j < length; ++j) {
                            if(this.mCursors[j] != null && !this.mCursors[j].isAfterLast()) {
                                long current = this.mCursors[j].getLong(this.mSortColumns[j]);
                                boolean comparison = this.mAscending?current < smallest:current > smallest;
                                if(smallestIdx < 0 || comparison) {
                                    smallest = current;
                                    smallestIdx = j;
                                }
                            }
                        }

                        if(i == newPosition) {
                            break;
                        }

                        if(this.mCursors[smallestIdx] != null) {
                            this.mCursors[smallestIdx].moveToNext();
                        }
                    }
                }

                this.mCursor = this.mCursors[smallestIdx];
                this.mCursorIndex = smallestIdx;
                this.mRowNumCache[cache_entry] = newPosition;
                this.mCursorCache[cache_entry] = smallestIdx;

                for(i = 0; i < length; ++i) {
                    if(this.mCursors[i] != null) {
                        this.mCurRowNumCache[cache_entry][i] = this.mCursors[i].getPosition();
                    }
                }

                this.mLastCacheHit = -1;
                return true;
            }
        }
    }

    public String getString(int column) {
        return this.mCursor.getString(column);
    }

    public short getShort(int column) {
        return this.mCursor.getShort(column);
    }

    public int getInt(int column) {
        return this.mCursor.getInt(column);
    }

    public long getLong(int column) {
        return this.mCursor.getLong(column);
    }

    public float getFloat(int column) {
        return this.mCursor.getFloat(column);
    }

    public double getDouble(int column) {
        return this.mCursor.getDouble(column);
    }

    public boolean isNull(int column) {
        return this.mCursor.isNull(column);
    }

    public byte[] getBlob(int column) {
        return this.mCursor.getBlob(column);
    }

    public String[] getColumnNames() {
        if(this.mCursor != null) {
            return this.mCursor.getColumnNames();
        } else {
            int length = this.mCursors.length;

            for(int i = 0; i < length; ++i) {
                if(this.mCursors[i] != null) {
                    return this.mCursors[i].getColumnNames();
                }
            }

            throw new IllegalStateException("No cursor that can return names");
        }
    }

    public void deactivate() {
        int length = this.mCursors.length;

        for(int i = 0; i < length; ++i) {
            if(this.mCursors[i] != null) {
                this.mCursors[i].deactivate();
            }
        }

    }

    public void close() {
        int length = this.mCursors.length;

        for(int i = 0; i < length; ++i) {
            if(this.mCursors[i] != null) {
                this.mCursors[i].close();
            }
        }

    }

    public void registerDataSetObserver(DataSetObserver observer) {
        int length = this.mCursors.length;

        for(int i = 0; i < length; ++i) {
            if(this.mCursors[i] != null) {
                this.mCursors[i].registerDataSetObserver(observer);
            }
        }

    }

    public void unregisterDataSetObserver(DataSetObserver observer) {
        int length = this.mCursors.length;

        for(int i = 0; i < length; ++i) {
            if(this.mCursors[i] != null) {
                this.mCursors[i].unregisterDataSetObserver(observer);
            }
        }

    }

    public boolean requery() {
        int length = this.mCursors.length;

        for(int i = 0; i < length; ++i) {
            if(this.mCursors[i] != null && !this.mCursors[i].requery()) {
                return false;
            }
        }

        return true;
    }

    public int getCurrentCursorIndex() {
        return this.mCursorIndex;
    }
}
